#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    manager = new QNetworkAccessManager(this);
    connect(manager,QNetworkAccessManager::finished,this,Widget::requestReply);
    setCursor(QCursor(QPixmap(":/image/cat.ico")));
    ui->textEdit->setCursor(QCursor(QPixmap(":/image/cat.ico")));
}

Widget::~Widget()
{
    delete ui;
}

void Widget::sentHttpsRequest(const QString &max_id)
{
    if(max_id != "")
        request_url = QString("https://weibo.com/ajax/statuses/buildComments?is_reload=1&id=%1&is_show_bulletin=2&is_mix=0&max_id=%2&count=10&uid=%3&fetch_level=0&locale=zh-CN").arg(ui->lineEdit->text(),max_id,ui->lineEdit_2->text());
    else
        request_url = QString("https://weibo.com/ajax/statuses/buildComments?is_reload=1&id=%1&is_show_bulletin=2&is_mix=0&count=10&uid=%2&fetch_level=0&locale=zh-CN").arg(ui->lineEdit->text(),ui->lineEdit_2->text());
    qDebug()<<request_url;
    QNetworkRequest request;
    request.setUrl(request_url);
    request.setRawHeader("cookie",ui->lineEdit->text().toUtf8());
    manager->get(request);
}

void Widget::on_toolButton_clicked()
{
    requested_time = 0;
    ui->progressBar->setValue(0);
    if(ui->lineEdit->text() == "" || ui->lineEdit_2->text() == "" || ui->label->text() == "" || ui->textEdit->toPlainText() == ""){
        QMessageBox::critical(this,"缺少必要字段","请将必要字段填写完整以进行数据爬取!");
        return ;
    }
    request_url = QString("https://weibo.com/ajax/statuses/buildComments?is_reload=1&id=%1&is_show_bulletin=2&is_mix=0&count=10&uid=%2&fetch_level=0&locale=zh-CN").arg(ui->lineEdit->text(),ui->lineEdit_2->text());
    QNetworkRequest request;
    request.setUrl(request_url);
    request.setRawHeader("cookie",ui->lineEdit->text().toUtf8());
    manager->get(request);
}

void Widget::requestReply(QNetworkReply *reply)
{
    QJsonDocument doc = QJsonDocument::fromJson(reply->readAll());
    QJsonObject obj = doc.object();
    QJsonArray data_arr = obj.value("data").toArray();
    for(int i = 0;(i<data_arr.size())&&(requested_time < ui->spinBox->value());++i){
        QFile file(file_dir+"/评论.csv");
        file.open(QIODevice::ReadWrite | QIODevice::Append | QIODevice::Text);
        QTextStream txtOutput(&file);
        txtOutput<<QString::number(requested_time+1)<<','<<data_arr.at(i).toObject().value("text").toString()<<'\n';
        ++requested_time;
        ui->progressBar->setValue((requested_time/ui->spinBox->value())*100);
        ui->textEdit_2->append(QString("第 %1 个评论获取成功").arg(requested_time));
        file.close();
    }
    if(requested_time < ui->spinBox->value())
        sentHttpsRequest(QString::number(obj.value("max_id").toDouble(),'f',0));
    else{
        ui->textEdit_2->append(QString("%1 个评论爬取成功!").arg(ui->spinBox->value()));
        ui->progressBar->setValue(100);
    }
}

void Widget::on_toolButton_2_clicked()
{
    file_dir = QFileDialog::getExistingDirectory(this,"");
    ui->label->setText(file_dir);
}
